package com.module.product.manager;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.Transaction;

import base.common.CUDEnum;
import base.dao.GenericHibernateDao;
import base.db.HibernateSessionFactory;
import base.manager.GenericHibernateManager;

import com.module.product.dao.AttrItemComboxDao;
import com.module.product.dao.AttrItemDao;
import com.module.product.dao.CategoryDao;
import com.module.product.vo.AttrItem;
import com.module.product.vo.Category;


/**
*
* 商品分类业务处理类
*
* @author  王勇云
* @version  [V1.0, Jul 22, 2011]
* @see  [相关类/方法]
* @since  [二代服务平台/商品分类]
*/
public class CategoryManager extends GenericHibernateManager<Category, String>{
    private static final CategoryManager instance = new CategoryManager();
    static Logger logger = Logger.getLogger(CategoryManager.class.getName());
    private CategoryManager(){}
    
    public synchronized static CategoryManager getInstance() {
        return instance;
    }
    CategoryDao dao = CategoryDao.getInstance();
    
    AttrItemDao adao = AttrItemDao.getInstance();
    
    AttrItemComboxDao comboxDao = AttrItemComboxDao.getInstance();
    
    public boolean add(Category obj) throws Exception
    {
    	boolean flag = false;
        if(StringUtils.isBlank(obj.getId())){//添加
            obj.setStatus(CUDEnum.C.toString());
            obj.setCreateTime(new Date());
        }else{
            obj.setStatus(CUDEnum.U.toString());
            obj.setUpdateTime(new Date());
        }
        return saveOrUpdate(obj);
    }
    public boolean delete(Category obj) throws Exception
    {
    	Session session =HibernateSessionFactory.getSession();
    	Transaction tx =session.beginTransaction();
    	boolean flag = false;
    	try{
	    		flag = dao.deleteObjs(session,obj);
	    		tx.commit();
	    	}catch(Exception e){
	    		  if (tx != null) {
	                  tx.rollback();
	              }
	    		  throw e;
	    		
	    	}finally{
	    		this.closeSession();
	    	}
        return flag;
    }
    
    public boolean checkProducByCategory(String cid)throws Exception{
    	try{
    		long count = dao.getProductByCategory(this.getSession(), cid);
    		if(count>0)
    			return true;
    		return false;
    	}catch (Exception e) {
			throw e;
		}finally{
			this.closeSession();
		}
    }
    
    public boolean update(Category obj) throws Exception
    {
    	Session session = HibernateSessionFactory.getSession();
    	boolean flag = false;
    	try{
    		flag =  dao.update(session,obj);
    	}catch(Exception e){
    		throw e;
    		
    	}finally{
    		this.closeSession();
    	}
    	return flag;
    }
    
    
    public List getCategoryTree(Category obj) throws Exception{
    	Session session =HibernateSessionFactory.getSession();
    	List<Category> list = null ;
    	try{
    		list = dao.getCategoryTree(session,obj.getName(),obj.getId());
	    }catch(Exception e){
	    	throw e;    		
	    }finally{
	    	this.closeSession();
	    }
        return list;
    }
    
    /**
     * 根据类型查询属性组
     * @param categoryId
     * @return
     * @throws SQLException
     */
    @SuppressWarnings("deprecation")
	public  List<AttrItem> listByCategoryId(String categoryId) throws Exception{
    	List<AttrItem> attrList  = new ArrayList<AttrItem>();
    	Session session =HibernateSessionFactory.getSession();
		Connection conn = session.connection();
		try{
			List<AttrItem> list =  adao.listByCategoryId(conn, categoryId);
			attrList = this.getComboxVaule(list, session);
		}catch (SQLException e) {
			throw e;
		}finally{
			this.closeSession();
		}
		return attrList;
	}
    
    private List<AttrItem> getComboxVaule(List<AttrItem> list,Session session) throws Exception{
		List<AttrItem> attrList  = new ArrayList<AttrItem>();	
		if(list != null && ! list.isEmpty()){
			int index = list.size();
			for(int i=0;i<index;i++){
				AttrItem atrrItem = list.get(i);
				List<String> comboxList = comboxDao.getComboxByAtrrId(session, atrrItem.getId());
				atrrItem.setComboxList(comboxList);
				attrList.add(atrrItem);
			}
		}		
		return  attrList;
	}

	@Override
	public GenericHibernateDao<Category, String> getDao() {
		// TODO Auto-generated method stub
		return dao;
	}
}
